package com.example.musicplayer;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationBuilderWithBuilderAccessor;

import android.Manifest;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.MediaScannerConnection;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SimpleAdapter;
import android.widget.Switch;
import android.widget.TextView;
import android.content.BroadcastReceiver;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private static final int REQUEST_PERMISSION = 0;

    //控件声明
    private ImageButton imgButton_Previous;
    private ImageButton imgButton_PlayOrPause;
    private ImageButton imgButton_Stop;
    private ImageButton imgButton_Next;
    private ListView musicList;
    private RelativeLayout layout;
    private TextView textView_MusicInfo;
    private SeekBar seekBar;
    private SeekBar seekBarVolume;
    private Handler seekBarHandler;

    private TextView musicDuration;
    private TextView musicPosition;

    private TextView Volume;

    private ArrayList<Music> musicArrayList;

    private int number = 0;
    public int status = 0;

    //播放模式
    private String[] playModeString = new String[]{"顺序播放","随机播放","列表循环","单曲循环"};
    private int playMode = 0;

    //循环模式键值对
    public static final int MODE_LISTSINGLE = 0;
    public static final int MODE_RAMDOM = 1;
    public static final int MODE_LISTCYCLE = 2;
    public static final int MODE_SINGLECYCLE = 3;

    //进度条SeekBar状态
    private static final int PROGRESS_INCREASE = 0;
    private static final int PROGRESS_PAUSE = 1;
    private static final int PROGRESS_RESET = 2;

    //随机播放生成的数组
    public int randomArray[];

    private int duration;
    private int time;

    //双击退出判断位
    private boolean quit = false;

    //音量SeekBar参数
    public int currentVolume = 0;
    public int max_progress = 0;

    //广播接收
    private StatusChangedReceiver receiver;

    public String currentTheme = "群山";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findView();
        registerListener();
        initMusicList();
        initListView();
        checkMusicFile();
        textView_MusicInfo.setSelected(true);

        bindStatusChangedReceiver();
        initSeekBarHandler();
        startService(new Intent(this, MusicService.class));
        status = MusicService.STATUS_STOP;
        audio_Control();

        //权限动态申请
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            int hasWritePermission = checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
            int hasReadPermission = checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE);

            List<String> permissions = new ArrayList<String>();
            if (hasWritePermission != PackageManager.PERMISSION_GRANTED) {
                permissions.add(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
            } else {

            }
            if (hasReadPermission != PackageManager.PERMISSION_GRANTED) {
                permissions.add(android.Manifest.permission.READ_EXTERNAL_STORAGE);

            } else {

            }
            if (!permissions.isEmpty()) {
                ActivityCompat.requestPermissions(this, new String[] {android.Manifest.permission
                                .WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_PHONE_STATE},
                        REQUEST_PERMISSION);
            }
        }



    }


    //动态权限申请
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_PERMISSION: {
                for (int i = 0; i < permissions.length; i++) {
                    if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {


                        System.out.println("Permissions --> " + "Permission Granted: " + permissions[i]);
                    } else if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
                        System.out.println("Permissions --> " + "Permission Denied: " + permissions[i]);
                    }
                }
            }
            break;
            default: {
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        sendBroadcastOnCommand(MusicService.COMMAND_CHECK_IS_PLAYING);

        Properbean properBean = new Properbean(MainActivity.this);
        String theme = properBean.getTheme();
        currentTheme = theme;
        setTheme(theme);
        audio_Control();

    }


    @Override
    protected void onStart() {
        super.onStart();
        musicPosition.setText(formatTime(time));
        seekBar.setProgress(time);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        if (status == MusicService.STATUS_STOP) {
            stopService(new Intent(this, MusicService.class));
        }
        super.onDestroy();
    }


    private void bindStatusChangedReceiver()
    {
        receiver = new StatusChangedReceiver();
        IntentFilter filter = new IntentFilter(MusicService.BROADCAST_MUSICSERVICE_UPDATE_STATUS);
        registerReceiver(receiver,filter);
    }



    private void findView() {

        imgButton_Previous = (ImageButton) findViewById(R.id.bt_previous);
        imgButton_PlayOrPause = (ImageButton) findViewById(R.id.bt_play);
        imgButton_Stop = (ImageButton) findViewById(R.id.bt_stop);
        imgButton_Next = (ImageButton) findViewById(R.id.bt_next);
        musicList = (ListView) findViewById(R.id.listView);
        layout = (RelativeLayout) findViewById(R.id.relativeLayout);
        textView_MusicInfo = (TextView) findViewById(R.id.musicInfo);
        musicDuration = (TextView) findViewById(R.id.musicDuration);
        musicPosition = (TextView) findViewById(R.id.musicPosition);
        Volume = (TextView) findViewById(R.id.textView_Volume);
        seekBar = (SeekBar) findViewById(R.id.seekBar);
        seekBarVolume = (SeekBar) findViewById(R.id.seekBar_Volume);

    }

    private void registerListener() {

        imgButton_Previous.setOnClickListener(this);
        imgButton_PlayOrPause.setOnClickListener(this);
        imgButton_Stop.setOnClickListener(this);
        imgButton_Next.setOnClickListener(this);
        musicList.setOnItemClickListener(this);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (status != MusicService.STATUS_STOP)
                {
                    time = progress;
                    musicPosition.setText(formatTime(time));
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                seekBarHandler.sendEmptyMessage(PROGRESS_PAUSE);
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if(status == MusicService.STATUS_PLAYING)
                {
                    sendBroadcastOnCommand(MusicService.COMMAND_SEEK_TO);
                    seekBarHandler.sendEmptyMessage(PROGRESS_INCREASE);
                }
                else if(status == MusicService.STATUS_PAUSED)
                {
                    sendBroadcastOnCommand(MusicService.COMMAND_SEEK_TO);
                }
            }
        });

        seekBarVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.bt_previous:
                sendBroadcastOnCommand(MusicService.COMMAND_PREVIOUS);
                break;

            case R.id.bt_play:
                switch (status)
                {
                    case MusicService.STATUS_PLAYING:
                        sendBroadcastOnCommand(MusicService.COMMAND_PAUSE);
                        break;

                    case MusicService.STATUS_PAUSED:
                        sendBroadcastOnCommand(MusicService.COMMAND_RESUME);
                        break;

                    case MusicService.STATUS_STOP:
                        sendBroadcastOnCommand(MusicService.COMMAND_PLAY);
                        break;
                }

                break;

            case R.id.bt_stop:
                sendBroadcastOnCommand(MusicService.COMMAND_STOP);
                break;

            case R.id.bt_next:
                sendBroadcastOnCommand(MusicService.COMMAND_NEXT);
                break;

        }

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        randomArray = new int[MusicList.getMusicArrayList().size()];
        number = position;
        randomArray = getRandom();
        sendBroadcastOnCommand(MusicService.COMMAND_PLAY);
        //imgButton_PlayOrPause.setBackgroundResource(R.drawable.button_pause);
    }

    //初始化音乐列表
    private void initMusicList() {
        musicArrayList = MusicList.getMusicArrayList();

        if(musicArrayList.isEmpty())
        {
            Cursor myMusicCursor = this.getContentResolver().query(
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
                    MediaStore.Audio.AudioColumns.TITLE);


            int indexTitle = myMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.TITLE);
            int indexArtist = myMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST);
            int indexTotalTime = myMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION);
            int indexPath = myMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA);

            for(myMusicCursor.moveToFirst();!myMusicCursor.isAfterLast();myMusicCursor.moveToNext())
            {
                String strTitle = myMusicCursor.getString(indexTitle);
                String strArtist = myMusicCursor.getString(indexArtist);
                String strDuration = myMusicCursor.getString(indexTotalTime);
                String strPath = myMusicCursor.getString(indexPath);

                if (strArtist.equals("<unknown>"))
                {
                    strArtist = "无艺术家";
                    Music music = new Music(strTitle, strArtist, strPath, strDuration);
                    musicArrayList.add(music);
                }else
                {
                    Music music = new Music(strTitle, strArtist, strPath, strDuration);
                    musicArrayList.add(music);
                }


            }

        }
    }


    private void initListView() {
        List<Map<String,String>> list_map = new ArrayList<Map<String, String>>();
        HashMap<String,String> map;
        SimpleAdapter simpleAdapter;

        //通过for循环用music在musicArrayList里面遍历一次
        for(Music music : musicArrayList )
        {
            map = new HashMap<String, String>();
            map.put("musicName",music.getMusicName());
            map.put("musicArtist",music.getMusicArtist());
            list_map.add(map);
        }

        String[] from = new String[] {"musicName","musicArtist"};
        int[] to = {R.id.item_title,R.id.item_artist};
        simpleAdapter = new SimpleAdapter(this,list_map,R.layout.listitem,from,to);
        musicList.setAdapter(simpleAdapter);

    }


    private void checkMusicFile()
    {
        if(musicArrayList.isEmpty())
        {
            imgButton_Next.setEnabled(false);
            imgButton_PlayOrPause.setEnabled(false);
            imgButton_Previous.setEnabled(false);
            imgButton_Stop.setEnabled(false);
            Toast.makeText(getApplicationContext(), R.string.tip_no_music_find ,Toast.LENGTH_SHORT).show();
        }
        else
        {
            imgButton_Next.setEnabled(true);
            imgButton_PlayOrPause.setEnabled(true);
            imgButton_Previous.setEnabled(true);
            imgButton_Stop.setEnabled(true);
        }
    }



    private void sendBroadcastOnCommand(int command)
    {

        Intent intent = new Intent(MusicService.BROADCAST_MUSICSERVICE_CONTROL);
        intent.putExtra("command",command);
        switch (command)
        {
            case MusicService.COMMAND_PLAY:
                intent.putExtra("number",number);
                intent.putExtra("randomArray",randomArray);
                break;

            case MusicService.COMMAND_PAUSE:
                break;

            case MusicService.COMMAND_STOP:
                break;

            case MusicService.COMMAND_RESUME:
                break;

            case MusicService.COMMAND_PREVIOUS:
                intent.putExtra("playMode",playMode);
                break;

            case MusicService.COMMAND_NEXT:
                intent.putExtra("playMode",playMode);
                break;

            case MusicService.COMMAND_SEEK_TO:
                intent.putExtra("time",time);
                break;

            case MusicService.COMMAND_MODE_CHANGE:
                intent.putExtra("playMode",playMode);

        }
        sendBroadcast(intent);

    }




    class StatusChangedReceiver extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent) {

            String musicName = intent.getStringExtra("musicName");
            String musicArtist = intent.getStringExtra("musicArtist");


            status = intent.getIntExtra("status",MusicService.COMMAND_UNKNOWN);
            switch (status)
            {
                case MusicService.STATUS_PLAYING:
                    imgButton_PlayOrPause.setBackgroundResource(R.drawable.button_pause);
                    MainActivity.this.setTitle("正在播放:" + musicName + " - " + musicArtist);
                    textView_MusicInfo.setText("正在播放:" + musicName + " - " + musicArtist);
                    number = intent.getIntExtra("number",number);

                    seekBarHandler.removeMessages(PROGRESS_INCREASE);
                    time = intent.getIntExtra("time",0);
                    duration = intent.getIntExtra("duration",0);
                    seekBar.setProgress(time);
                    seekBar.setMax(duration);
                    seekBarHandler.sendEmptyMessage(PROGRESS_INCREASE);
                    musicDuration.setText(formatTime(duration));


                    break;

                case MusicService.STATUS_PAUSED:
                    imgButton_PlayOrPause.setBackgroundResource(R.drawable.button_play);
                    MainActivity.this.setTitle(musicName + " - " + musicArtist);
                    textView_MusicInfo.setText(musicName + " - " + musicArtist);
                    //number = intent.getIntExtra("number",0);
                    seekBarHandler.sendEmptyMessage(PROGRESS_PAUSE);
                    break;

                case MusicService.STATUS_STOP:
                    time = 0;
                    duration = 0;
                    musicPosition.setText(formatTime(time));
                    musicDuration.setText(formatTime(duration));
                    seekBarHandler.sendEmptyMessage(PROGRESS_RESET);

                    imgButton_PlayOrPause.setBackgroundResource(R.drawable.button_play);
                    MainActivity.this.setTitle("");
                    textView_MusicInfo.setText("");
                    //number = intent.getIntExtra("number",0);

                    break;

                case MusicService.STATUS_COMPLETED:

                    if(playMode == MODE_SINGLECYCLE)        //单曲循环
                    {
                        number = intent.getIntExtra("number",0);
                        sendBroadcastOnCommand(MusicService.COMMAND_PLAY);
                        imgButton_PlayOrPause.setBackgroundResource(R.drawable.button_play);
                        MainActivity.this.setTitle("");
                        textView_MusicInfo.setText("");
                        break;
                    }
                    else if(playMode == MODE_LISTSINGLE)        //顺序播放
                    {
                        if(number == MusicList.getMusicArrayList().size()-1)
                        {
                            number = intent.getIntExtra("number",0);
                            imgButton_PlayOrPause.setBackgroundResource(R.drawable.button_play);
                            sendBroadcastOnCommand(MusicService.COMMAND_STOP);
                            MainActivity.this.setTitle("");
                            textView_MusicInfo.setText("");
                            break;
                        }
                        else
                        {
                            number = intent.getIntExtra("number",0);
                            sendBroadcastOnCommand(MusicService.COMMAND_NEXT);
                            imgButton_PlayOrPause.setBackgroundResource(R.drawable.button_play);
                            MainActivity.this.setTitle("");
                            textView_MusicInfo.setText("");
                            break;
                        }

                    }
                    else if (playMode == MODE_LISTCYCLE)        //列表循环
                    {
                        if(number == MusicList.getMusicArrayList().size()-1)
                        {
                            number = 0;
                            sendBroadcastOnCommand(MusicService.COMMAND_PLAY);
                        }
                    }
                    else if (playMode == MODE_RAMDOM)
                    {
                        sendBroadcastOnCommand(MusicService.COMMAND_NEXT);
                    }


                default:
                    break;
            }
        }
    }


    private void setTheme(String theme)
    {

        if ("默认".equals(theme)) {
            layout.setBackgroundResource(R.drawable.bg_background);
        } else if ("幸运".equals(theme)) {
            layout.setBackgroundResource(R.drawable.luck);
        } else if ("风景".equals(theme)) {
            layout.setBackgroundResource(R.drawable.view);
        } else if ("简约".equals(theme)) {
            layout.setBackgroundResource(R.drawable.bg_background1);
        }
    }


    /**
     * 创建菜单
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        this.getMenuInflater().inflate(R.menu.menu,menu);
        return true;
    }


    //menu点击事件监听函数
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId())
        {
            case R.id.menu_theme:
                new AlertDialog.Builder(this)
                        .setTitle("请选择主题")
                        .setItems(R.array.theme,new android.content
                                .DialogInterface.OnClickListener(){
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String theme = Properbean .THEMES[which];
                                currentTheme = theme;
                                MainActivity.this.setTheme(theme);
                                Properbean propertyBean = new Properbean(MainActivity.this);
                                propertyBean.setAndSaveTheme(theme);
                            }
                        }).show();
                //Toast.makeText(this,"111",Toast.LENGTH_SHORT).show();
                break;

            case R.id.menu_about:
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("GracePlayer")
                        .setMessage(R.string.about).show();
                break;

            case R.id.menu_quit:
                new AlertDialog.Builder(this)
                        .setTitle("提示").setMessage("退出").setMessage("退出").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        System.exit(0);
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).show();
                break;

            case R.id.menu_playmode:
                //Toast.makeText(MainActivity.this,"1111",Toast.LENGTH_SHORT).show();
                new AlertDialog.Builder(this)
                        .setTitle("请选择播放模式 ")
                        .setSingleChoiceItems(playModeString,playMode,new DialogInterface.OnClickListener()
                        {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                switch (which)
                                {
                                    case MODE_LISTSINGLE:
                                        modeChange(MODE_LISTSINGLE);
                                        dialog.dismiss();
                                        break;
                                    case MODE_RAMDOM:
                                        modeChange(MODE_RAMDOM);
                                        dialog.dismiss();
                                        break;
                                    case MODE_LISTCYCLE:
                                        modeChange(MODE_LISTCYCLE);
                                        dialog.dismiss();
                                        break;
                                    case MODE_SINGLECYCLE:
                                        modeChange(MODE_SINGLECYCLE);
                                        dialog.dismiss();
                                        break;
                                }
                            }
                        }).show();
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * 歌曲时间毫秒转换函数
     * @param msec
     * @return
     */
    private String formatTime(int msec)
    {
        int minute = msec / 1000 / 60;
        int second = msec /1000 % 60;
        String minuteString;
        String secondString;
        if(minute < 10)
        {
            minuteString = "0" + minute;
        }
        else
        {
            minuteString = "" + minute;
        }

        if(second < 10)
        {
            secondString = "0" + second;
        }
        else
        {
            secondString = "" + second;
        }

        return minuteString + ":" + secondString;
    }


    /**
     * 进度条Handler处理机制
     */
    private void initSeekBarHandler()
    {
        seekBarHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what)
                {
                    case PROGRESS_INCREASE:
                        if(seekBar.getProgress() < duration)
                        {
                            seekBar.setProgress(time);
                            musicPosition.setText(formatTime(time));
                            time += 1000;
                            seekBarHandler.sendEmptyMessageDelayed(PROGRESS_INCREASE, 1000);
                        }

                        break;

                    case PROGRESS_PAUSE:
                        seekBarHandler.removeMessages(PROGRESS_INCREASE);
                        break;

                    case PROGRESS_RESET:
                        seekBarHandler.removeMessages(PROGRESS_INCREASE);
                        seekBar.setProgress(0);
                        musicPosition.setText("00:00");
                        break;
                }
            }
        };


    }


    /**
     * 系统按键监听函数
     */

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            exitByDoubleClick();
        }
        else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
        {
            if(currentVolume != 0)
            {
                currentVolume = seekBarVolume.getProgress();
                seekBarVolume.setProgress(currentVolume-(max_progress/5));
            }
            else
            {
                Toast.makeText(getApplicationContext(),"静音",Toast.LENGTH_SHORT).show();
            }

        }
        else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP)
        {
            if(currentVolume != max_progress)
            {
                currentVolume = seekBarVolume.getProgress();
                seekBarVolume.setProgress(currentVolume+(max_progress/5));
            }
            else
            {
                Toast.makeText(getApplicationContext(),"已达到最大音量",Toast.LENGTH_SHORT).show();
            }
        }

        return false;
    }

    /**
     * 双击返回退出软件方法
     */
    public void exitByDoubleClick()
    {
        Timer timer = null;
        if(!quit)
        {
            quit = true;
            Toast.makeText(MainActivity.this,"点击两次返回键退出软件",Toast.LENGTH_SHORT).show();
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    quit = false;
                }
            },2000);
        }
        else
        {
            System.exit(0);
        }
    }

    //播放模式切换函数
    public void modeChange(int mode)
    {
        playMode = mode;
        sendBroadcastOnCommand(MusicService.COMMAND_MODE_CHANGE);
    }

    /**
     * 随机播放列表生成【算法】
     *
     * @return  随机播放列表
     */
    public int[] getRandom() {
        Random random = new Random();
        int startArray[] = new int[MusicList.getMusicArrayList().size()];
        int randomArray[] = new int[MusicList.getMusicArrayList().size()];

        for (int j = 0; j < MusicList.getMusicArrayList().size(); j++) {
            startArray[j] = j;
        }

        for (int i = 0; i < MusicList.getMusicArrayList().size(); i++) {
            int seed = random.nextInt(MusicList.getMusicArrayList().size() - i);//从剩下的随机数里生成
            randomArray[i] = startArray[seed];//赋值给结果数组
            startArray[seed] = startArray[MusicList.getMusicArrayList().size() - i - 1];//把随机数产生过的位置替换为未被选中的值。
        }
        return randomArray;
    }


    /**
     * 音量控制
     */
    public void audio_Control()
    {
        final AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);

        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);

        max_progress = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)-1;

        seekBarVolume.setMax(max_progress);

        int progress = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

        seekBarVolume.setProgress(progress);

        Volume.setText("当前音量："+(progress*100/max_progress)+"%");

        currentVolume = progress;

        seekBarVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Volume.setText("当前音量："+(progress*100)/max_progress+"%");
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,progress,AudioManager.FLAG_PLAY_SOUND);
                currentVolume = progress;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }
}